//
//  ForgetPasswordNextViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/1/14.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "ForgetPasswordNextViewController.h"
#import "LoginViewController.h"
@interface ForgetPasswordNextViewController ()<UITextFieldDelegate>
{
    NSMutableString * passWordStr;
}
@end

@implementation ForgetPasswordNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
    // Do any additional setup after loading the view.
}
-(void)main
{
    passWordStr = [[NSMutableString alloc]init];

    self.view.backgroundColor = [UIColor whiteColor];
    //背景图
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView * bgImgV = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    bgImgV.image = [UIImage imageNamed:@"登录"];
    [self.view addSubview:bgImgV];
    //上面2
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 20)];
    titleLab.text = @"重置密码";
    titleLab.textColor = UIColorFromRGB(0xFFFFFF);
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.font = [UIFont systemFontOfSize:23];
    [self.view addSubview:titleLab];
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10, 30, 30, 20);
    [leftBtn setImage:[UIImage imageNamed:@"上月"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnActoin:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftBtn];
    
    //textField
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(SIZETO6W(29), titleLab.bottom+SIZETO6H(53), SCREEN_WIDTH-SIZETO6W(29)*2, 47)];
    view.backgroundColor = [UIColor clearColor];
    view.layer.masksToBounds = YES;
    view.layer.borderColor = [UIColorFromRGB(0xFFFFFF) CGColor];
    view.layer.borderWidth = 1.0f;
    view.layer.cornerRadius = 20;
    [self.view addSubview:view];
    

    UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(SIZETO6W(20), 0, view.width-SIZETO6W(20)-SIZETO6W(15)-15, 49)];
    UIColor * color = UIColorFromRGB(0xffffff);
    textField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"请输入6-16位新密码" attributes:@{NSForegroundColorAttributeName:color}];
    textField.tag = 100;
    textField.delegate = self;
    textField.secureTextEntry = YES;
    textField.textColor = color;
    [view addSubview:textField];
    [textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 30, 30);
    [btn setImage:[UIImage imageNamed:@"关闭按钮-"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clearBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    textField.rightView = btn;
    textField.rightViewMode = UITextFieldViewModeWhileEditing;
    textField.keyboardType = UIKeyboardTypeASCIICapable;

    UIButton * lastBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    lastBtn.frame = CGRectMake(SIZETO6W(29), view.bottom+SIZETO6H(51), SCREEN_WIDTH-SIZETO6W(29)*2, 47);
    [lastBtn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
    [lastBtn setTitle:@"下一步" forState:UIControlStateNormal];
    lastBtn.backgroundColor = UIColorFromRGB(0x6ec844);
    lastBtn.layer.masksToBounds = YES;
    lastBtn.layer.cornerRadius = 20;
    [lastBtn addTarget:self action:@selector(lastBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:lastBtn];

}
-(void)textChange:(UITextField *)textField
{
    if (textField.text.length > passWordStr.length) {
        if (textField.text.length <= 16) {
            
            NSString * s = [textField.text substringWithRange:NSMakeRange(textField.text.length-1, 1)];
            if ([s isEqualToString:@"&"]) {
                
            }
            else
            {
                [passWordStr appendString:s];
            }
            
        }
    }
    else if([textField.text isEqualToString:@""])
    {
        passWordStr = [[NSMutableString alloc]init];
    }
    else
    {
        NSString * str = [passWordStr substringToIndex:passWordStr.length-1];
        passWordStr = [[NSMutableString alloc]initWithString:str];
    }
    textField.text = passWordStr;
}

#pragma mark - btn的点击事件们
-(void)lastBtnAction:(UIButton *)btn
{
    
    
    
    
    __weak typeof(self)weakSelf = self;
    UITextField * textField = (UITextField *)[self.view viewWithTag:100];
    if (![textField.text isEqualToString:@""] && textField.text.length >=6 && textField.text.length <=16) {
        
        [U76HONet setNewPasswordWithAction:@"" Username:_mobile Userpwd:textField.text andDataBlock:^(NSDictionary *dic) {
            [Singleton alerSendMessage:@"修改密码成功"];
            
            [weakSelf performSelector:@selector(toLogin) withObject:nil afterDelay:1];
            
        }];
        
    }
    else
    {
        [Singleton alerSendMessage:@"6-16位密码"];
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if ([string isEqualToString:@"\n"]) {
        //        NSLog(@"开始登录了");
        
        __weak typeof(self)weakSelf = self;
        UITextField * textField = (UITextField *)[self.view viewWithTag:100];
        if (![textField.text isEqualToString:@""] && textField.text.length >=6 && textField.text.length <=16) {
            
            [U76HONet setNewPasswordWithAction:@"" Username:_mobile Userpwd:textField.text andDataBlock:^(NSDictionary *dic) {
                [Singleton alerSendMessage:@"修改密码成功"];
                
                [weakSelf performSelector:@selector(toLogin) withObject:nil afterDelay:1];
                
            }];
            
        }
        else
        {
            [Singleton alerSendMessage:@"6-16位密码"];
        }

        return NO;
    }
    return YES;
    
    
    
    
}

-(void)toLogin
{
    
    
    for (UIViewController * vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[LoginViewController class]]) {
            [self.navigationController popToViewController:vc animated:YES];
        }
    }
}
-(void)leftBtnActoin:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)clearBtnClick:(id)sender
{
    UITextField * textField = (UITextField *)[self.view viewWithTag:100];
    textField.text = @"";
    passWordStr = [[NSMutableString alloc]init];

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
